package cn.tsinghua.comsmacitccntsinghua.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import org.w3c.dom.Text;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import cn.tsinghua.comsmacitccntsinghua.DeviceInfo;
import cn.tsinghua.comsmacitccntsinghua.R;
import cn.tsinghua.comsmacitccntsinghua.VO.status.Status;
import cn.tsinghua.comsmacitccntsinghua.tsinterface.BinStatus;
import lombok.SneakyThrows;

/**
 * @Author yjl
 * @create 2021/9/24 8:57
 */
public class BinItemView extends LinearLayout {

    public static final String TAG = "BinItemView";
    private ImageView binImage;
    private TextView binStatusText;
    private Status status = new Status(); //每个桶子有唯一的status
    private TextView coinsText;
    //初始化 代码创建 刚开始始终是绿色
    public BinItemView(Context context) {
        super(context);
        initView(context);
        //默认初始化成正常情况
        status.setStatus(Status.normal_normal);
        status.setType(Status.NORMAL);
        setTextStyle(status.getType());
        //默认绿色
    }
    //XML创建
    public BinItemView(Context context, @Nullable AttributeSet attrs) throws IOException {
        super(context, attrs);
        initView(context);
        TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.BinItemView);
        Integer status = attributes.getInteger(R.styleable.BinItemView_status,0);
        int bmp = attributes.getResourceId(R.styleable.BinItemView_img,-1);
        if(bmp != -1){
            setBinImageBitMap(R.mipmap.yellow_close);
        }else {
            setBinImageBitMap(R.mipmap.green_close);
        }
        setTextStyle(status);
    }
    public BinItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.BinItemView);
        Integer status = attributes.getInteger(R.styleable.BinItemView_status,0);
        setTextStyle(status);
    }


    private void initView(Context context){
        View.inflate(context, R.layout.fragment_bin,this);
        binImage = (ImageView)this.findViewById(R.id.fra_iv_dustbin_img);
        binStatusText = (TextView)this.findViewById(R.id.fra_tv_dustbin);
        coinsText = (TextView) this.findViewById(R.id.coins_tv);
    }



    //设置textView样式
    private void setTextStyle(int status){
        if(status == Status.NORMAL){
            binStatusText.setText(R.string.normal);
            binStatusText.setBackgroundResource(R.drawable.bin_status_shape);
        }else if(status == Status.ERROR){
            binStatusText.setText(R.string.error);
            binStatusText.setBackgroundResource(R.drawable.bin_lost_status_shape);
        }else if(status == Status.WARN){
            binStatusText.setText(R.string.warn);
            binStatusText.setBackgroundResource(R.drawable.bin_full_status_shape);
        }else if(status == Status.FULL){
            binStatusText.setText(R.string.full);
            binStatusText.setBackgroundResource(R.drawable.bin_full_status_shape);
        }else if(status == Status.CANOTOPEN){
            binStatusText.setText(R.string.cannot_open);//未启用
            binStatusText.setBackgroundResource(R.drawable.bin_cannot_open_status_shape);
        }
    }

    public void setBinImageBitMap(int id){
        binImage.setBackgroundResource(id);
    }


    //TODO Satus->JSON
    //由HTTP获取某个桶子的Status 并且拼接成接口
    public Status getStatus() {
        return status;
    }

    public void setCoinsText(String coinText){
        coinsText.setText(coinText);
    }

    //由底层硬件设置status
    public void setStatus(Status status) {
        if(status.getType() == 0){
            setTextStyle(Status.NORMAL);
        }else if(status.getType() == 1){
            setTextStyle(Status.WARN);
        } else if(status.getType() == 2){
            setTextStyle(Status.ERROR);
        }else if(status.getType() == 3){
            setTextStyle(Status.FULL);
        }else if(status.getType() == 4){
            setTextStyle(Status.CANOTOPEN);
        }
        this.status = status;
    }

    public Drawable getDrawable(String url) throws IOException {
//        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id);
        Drawable drawable = null;
        if(null!=url && !url.equals("")){
            //AsyncTask获取URL
            try{
                Log.d(TAG,"getDrawable() ----- 获取图片");
                //TODO 获取到图片
//                drawable = Drawable.createFromStream(new URL(url).openStream(),"image.jpg");
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
//                Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openStream());
                Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openStream(),null,options);
                drawable = new BitmapDrawable(bitmap);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return drawable;
    }


    //从后端拿图
    public void setNetDrawable(String url){
        Log.d(TAG,"setNetDrawable ----- 从流中拿图");
        Log.d(TAG,"图片url " + url);
        new DownloadImageTask().execute(url); //请求成功直接在onPostExecute中设置了
    }


    private class DownloadImageTask extends AsyncTask<String,Void,Drawable>{
        @SneakyThrows
        @Override
        protected Drawable doInBackground(String... urls) {
            return getDrawable(urls[0]);
        }

        @Override
        protected void onPostExecute(Drawable drawable) {
            //drwable转bitmap
            //设置圆角 (背景的方式)
            if(DeviceInfo.mouthQuantity == 5){
                binImage.setBackgroundResource(R.drawable.image_shape);
            }else {
                binImage.setBackgroundResource(R.drawable.image_4mouth_shape);
            }
            BitmapDrawable bd = (BitmapDrawable)drawable;
            if(bd == null){
                return;
            }


            binImage.setImageBitmap(bd.getBitmap());
//            binImage.setBackground(drawable);
        }
    }
}
